<?php
//自动生成,请根据需要修改
namespace app\log\controller;

use app\common\controller\Base;
use think\Db;
use think\db\Where;

/**
 * @title 轮播图页面页面（模块08-01）
 * @description 轮播图页面说明
 * @group 轮播图（08）
 * @header name:model_id require:0 default:08-01 desc:模块
 * @groupremark 已使用错误码：1-08-01-0001 校验失败 1-08-01-0002 添加失败 1-08-01-0003 编辑校验失败 1-08-01-0004 编辑失败 1-08-01-0005 获取详情失败  1-08-01-0006 获取列表失败 1-08-01-0007获取单个详情失败 1-08-01-0008 删除失败
 */
class Log extends Base
{

    /**
     * @title 查询日志列表页面
     * @description 查询日志列表页面
     * @author 李正
     * @url /log/log/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:日志列表@
     */
    public function getlist()
    {
        $Elas = new \app\common\controller\Elasticsearch();
        $data = input();
        foreach ($data as $k => $v) {
            if ($v === 0) {
                continue;
            }
            if (empty($v)) {
                unset($data[$k]);
            }
        }
        if (isset($data['name']) && $data['name'] && !empty($data['name'])) {
            $where['name'] = $data['name'];
            $Elas->WhereElastice($where);  //查询条件  == mysql的where 但是只能查询一个条件
        }
        $res = $Elas->getElastice(true);
        if(empty($res)){
            $res['data'] = [];
        }
        array_multisort(array_column($res['data'],'addtime'),SORT_DESC,$res['data']);
        $msg['list'] = $res;
        return $this->successJson($msg);

    }
    /**
     * @title 删除日志
     * @description 删除日志
     * @author 李正
     * @url /log/log/del
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:删除成功 --  失败@
     */
    public function del()
    {
        $Elas = new \app\common\controller\Elasticsearch();
        $data = input('id');
        $res = $Elas->delElastice($data);
        if ($res) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            return $this->errorJson($msg);
        }
    }

    public function add()
    {
        $data = input();
        //过滤数据
        $key = ['name'=>1,'content'=>1,'system'=>1,'module'=>1,'func'=>1];
        foreach($data as $k => $v){
            if(!array_key_exists($k,$key)){
                unset($data[$k]);
            }
        }
        $Elas = new \app\common\controller\Elasticsearch();
        $res = $Elas->addSingleElastics($data);
        $msg['msg'] = '插入日志成功';
        return $this->successJson($msg);
    }
}